package com.getpebble.android.framework.firmware;

import android.content.ContentValues;
import com.getpebble.android.common.model.FirmwareVersion;
import com.getpebble.android.common.model.HardwarePlatform;
import com.google.common.primitives.UnsignedInteger;
import com.google.gson.annotations.SerializedName;

public class FirmwareManifestBundle {
    private HardwarePlatform mHardwarePlatform;
    private int mId;
    private FirmwareMetadata mNormalMetadata;
    private FirmwareMetadata mRecoveryMetadata;
    private ReleaseType mReleaseType;

    public class FirmwareMetadata {
        private String friendlyVersion;
        private String notes;
        @SerializedName("sha-256")
        private String sha256;
        private long timestamp;
        private String url;

        public String getUrl() {
            return this.url;
        }

        public long getTimestamp() {
            return this.timestamp;
        }

        public String getNotes() {
            return this.notes;
        }

        public FirmwareVersion getFriendlyVersion() {
            return new FirmwareVersion(this.friendlyVersion, this.timestamp);
        }

        public String getSha256() {
            return this.sha256;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }

        public void setNotes(String notes) {
            this.notes = notes;
        }

        public void setFriendlyVersion(String friendlyVersion) {
            this.friendlyVersion = friendlyVersion;
        }

        public void setSha256(String sha256) {
            this.sha256 = sha256;
        }
    }

    public enum ReleaseType {
        INTERNAL("internal"),
        NIGHTLY("nightly"),
        ALPHA("alpha"),
        BETA("beta"),
        RELEASE_V2("release-v2"),
        UNKNOWN("unknown");
        
        public final String mPath;

        private ReleaseType(String path) {
            this.mPath = path;
        }

        public String getPath() {
            return this.mPath;
        }

        public static ReleaseType fromString(String s) {
            for (ReleaseType r : values()) {
                if (r.getPath().equalsIgnoreCase(s)) {
                    return r;
                }
            }
            return UNKNOWN;
        }
    }

    public FirmwareManifestBundle(HardwarePlatform hardwarePlatform, ReleaseType releaseType) {
        this.mHardwarePlatform = hardwarePlatform;
        this.mReleaseType = releaseType;
    }

    public void setId(int id) {
        this.mId = id;
    }

    public void setRecoveryMetadata(FirmwareMetadata recoveryMetadata) {
        this.mRecoveryMetadata = recoveryMetadata;
    }

    public void setNormalMetadata(FirmwareMetadata normalMetadata) {
        this.mNormalMetadata = normalMetadata;
    }

    public int getId() {
        return this.mId;
    }

    public HardwarePlatform getHardwarePlatform() {
        return this.mHardwarePlatform;
    }

    public ReleaseType getReleaseType() {
        return this.mReleaseType;
    }

    public FirmwareMetadata getRecoveryMetadata() {
        return this.mRecoveryMetadata;
    }

    public FirmwareMetadata getNormalMetadata() {
        return this.mNormalMetadata;
    }

    public ContentValues getContentValues() {
        ContentValues contentValues = new ContentValues(12);
        contentValues.put("hw_platform", Integer.valueOf(getHardwarePlatform().getId()));
        contentValues.put("release_type", getReleaseType().getPath());
        if (getNormalMetadata() != null) {
            contentValues.put("normal_url", getNormalMetadata().getUrl());
            contentValues.put("normal_timestamp", Long.valueOf(getNormalMetadata().getTimestamp()));
            contentValues.put("normal_notes", getNormalMetadata().getNotes());
            if (getNormalMetadata().getFriendlyVersion() != null) {
                contentValues.put("normal_version", getNormalMetadata().getFriendlyVersion().toString());
            }
            contentValues.put("normal_SHA256", getNormalMetadata().getSha256());
        }
        if (getRecoveryMetadata() != null) {
            contentValues.put("recovery_url", getRecoveryMetadata().getUrl());
            contentValues.put("recovery_timestamp", Long.valueOf(getRecoveryMetadata().getTimestamp()));
            contentValues.put("recovery_notes", getRecoveryMetadata().getNotes());
            if (getRecoveryMetadata().getFriendlyVersion() != null) {
                contentValues.put("recovery_version", getRecoveryMetadata().getFriendlyVersion().toString());
            }
            contentValues.put("recovery_SHA256", getRecoveryMetadata().getSha256());
        }
        return contentValues;
    }

    public static FirmwareManifestBundle fromContentValues(ContentValues contentValues) {
        FirmwareManifestBundle bundle = new FirmwareManifestBundle(HardwarePlatform.fromInt(UnsignedInteger.fromIntBits(contentValues.getAsInteger("hw_platform").intValue())), ReleaseType.fromString(contentValues.getAsString("release_type")));
        bundle.getClass();
        FirmwareMetadata normalMetadata = new FirmwareMetadata();
        normalMetadata.setUrl(contentValues.getAsString("normal_url"));
        normalMetadata.setTimestamp(contentValues.getAsLong("normal_timestamp").longValue());
        normalMetadata.setNotes(contentValues.getAsString("normal_notes"));
        normalMetadata.setFriendlyVersion(contentValues.getAsString("normal_version"));
        normalMetadata.setSha256(contentValues.getAsString("normal_SHA256"));
        bundle.getClass();
        FirmwareMetadata recoveryMetadata = new FirmwareMetadata();
        if (contentValues.containsKey("recovery_url")) {
            recoveryMetadata.setUrl(contentValues.getAsString("recovery_url"));
            recoveryMetadata.setTimestamp(contentValues.getAsLong("recovery_timestamp").longValue());
            recoveryMetadata.setNotes(contentValues.getAsString("recovery_notes"));
            recoveryMetadata.setFriendlyVersion(contentValues.getAsString("recovery_version"));
            recoveryMetadata.setSha256(contentValues.getAsString("recovery_SHA256"));
        }
        bundle.setId(contentValues.getAsInteger("_id").intValue());
        bundle.setNormalMetadata(normalMetadata);
        bundle.setRecoveryMetadata(recoveryMetadata);
        return bundle;
    }
}
